package com.shop.admin.controller;

import com.shop.admin.constant.IndentConstant;
import com.shop.admin.dto.DeliveryDTO;
import com.shop.admin.entity.ShopIndent;
import com.shop.admin.result.ApiResult;
import com.shop.admin.result.ApiResultList;
import com.shop.admin.service.ShopAdminService;
import com.shop.admin.service.ShopIndentService;
import com.shop.admin.utils.PageUtils;
import com.shop.admin.valid.UpdateGroup;
import com.shop.admin.vo.AdminVo;
import com.shop.admin.vo.IndentStatusVo;
import com.shop.admin.vo.IndentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.val;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = {"ShopIndentController"},description = "零食订单接口")
@RestController
@RequestMapping("/api/indent")
public class ShopIndentController {

    @Resource
    private ShopIndentService shopIndentService;

    @Resource
    private ShopAdminService shopAdminService;

    private Map<String, Object> params = new HashMap<>();

    private List<IndentVo> indentVoListist = new ArrayList<>();


    /**
     * 列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "post订单列表",notes = "page,limit,订单号，价格区间，用户姓名，用户手机号,组织id")
    public ApiResult list(@ApiParam("参数为：page,limit,indentCode,prices,userName,phonenumber,orgId")@RequestBody Map<String, Object> params){
        this.params = params;
        PageUtils queryPage = shopIndentService.queryPage(params);
        List<IndentVo> list = (List<IndentVo>) queryPage.getList();
        this.indentVoListist=list;
        return ApiResult.ok(queryPage);
    }

//    @Component
//    public class OrderPoller {
//        @Scheduled(initialDelay = 1000, fixedDelay = 20000)
//        public void pollOrders() {
//            System.out.println(params);
//            if (!params.isEmpty()) {
//                System.out.println("执行条件");
//                PageUtils queryPage = shopIndentService.queryPage(params);
//                List<IndentVo> queryPageList = (List<IndentVo>) queryPage.getList();
//                System.out.println(!indentVoListist.equals(queryPageList));
//                if (!indentVoListist.equals(queryPageList)) {
//                    indentVoListist = queryPageList;
//                System.out.println(indentVoListist);
//                    //simpMessagingTemplate.convertAndSend("/app/topicIndent", indentVoListist);
//                    simpMessagingTemplate.convertAndSendToUser("indent", "/app/topicIndent", indentVoListist);
//                }
//            }else {
//                System.out.println("执行无条件");
//                    HashMap<String, Object> hashMap = new HashMap<>();
//                    hashMap.put("page",1);
//                    hashMap.put("limit",10);
//
//                    PageUtils queryPage = shopIndentService.queryPage(hashMap);
//                    List<IndentVo> queryPageList = (List<IndentVo>) queryPage.getList();
//                System.out.println(queryPageList);
//                    if (!indentVoListist.equals(queryPageList)) {
//                        indentVoListist = queryPageList;
//                        System.out.println("----!"+indentVoListist);
//                        simpMessagingTemplate.convertAndSendToUser("indent", "/app/topicIndent", indentVoListist);
//                    }
//                }
//            }
//    }

    /**
     * 修改状态
     */
    @PostMapping("/update/status")
    @ApiOperation(value = "修改状态",notes = "状态，订单id")
    public ApiResult update(@ApiParam("参数为：indentStatus，indentId")@Validated(UpdateGroup.class) /**@RequestParam("indentStatus") Integer indentStatus,
                            @RequestParam("indentId") Long indentId*/@RequestBody IndentStatusVo vo){
        ShopIndent indentServiceById = shopIndentService.getById(vo.getIndentId());
        if (indentServiceById.getIndentStatus().equals(IndentConstant.StatusEnum.FINISH.getCode())){
            return ApiResult.ok("完成配送的订单无法在更改！");
        }
        if (vo.getIndentStatus() == IndentConstant.StatusEnum.FINISH.getCode()){
            ShopIndent byId = shopIndentService.getById(vo.getIndentId());
            if (byId.getIndentStatus() != IndentConstant.StatusEnum.ASSIGNED.getCode()){
                return ApiResult.ok("此状态不可完成！");
            }
        }

        shopIndentService.updateStatus(vo);
        return ApiResult.ok("修改成功！");
    }



    /**
     * 管理员列表
     */
    @GetMapping("/adminlist")
    @ApiOperation(value = "get管理员列表",notes = "管理员id,管理员姓名")
    public ApiResultList adminList(@ApiParam("参数为:")@RequestParam Map<String, Object> params){
        List<AdminVo> vos = shopAdminService.adminlist(params);
        return ApiResultList.ok(vos);
    }

    /**
     * 分配管理员
     */
    @PostMapping("/delivery")
    @ApiOperation(value = "post分配管理员",notes = "订单id,管理员id")
    public ApiResult delivery(@ApiParam("参数为：indentId,adminId")@RequestBody DeliveryDTO dto){
        shopIndentService.saveOrUpdateAdmin(dto);
        return ApiResult.ok("分配成功！");
    }
}
